<template>
  <div class="form">
    <h3>来 , 说说你在做什么 , 想什么</h3>
    <div class="user">
        <input type="text" placeholder="输入昵称" v-model.trim="form.username">
        <Avatar 
        :avatarIndex="avatarIndex" 
        :avatarlist="avatarList"
        @change="avatarIndex=$event"
        />
    </div>
    <div class="desc">
        <textarea placeholder="输入留言" v-model.trim="form.message"></textarea>
    </div>
    <div class="btn-wrap">
        <p v-if="this.form.message.length <= 140">还能输入<span>{{textNum}}</span>个字</p>
        <p v-else>超出<span style="color:red">{{textNum}}</span>个字</p>
        <button @click="submit">广播</button>
    </div>
  </div>
</template>

<script>
import Avatar from './Avatar.vue'
export default {
    data() {
        return {
            avatarIndex:0,
            form: {
                username:'',
                message:''
            },
            avatarList: [
            "https://www.jq22.com/demo/jquery-lyb20151124/img/face1.gif",
            "https://www.jq22.com/demo/jquery-lyb20151124/img/face2.gif",
            "https://www.jq22.com/demo/jquery-lyb20151124/img/face3.gif",
            "https://www.jq22.com/demo/jquery-lyb20151124/img/face4.gif",
            "https://www.jq22.com/demo/jquery-lyb20151124/img/face5.gif",
            "https://www.jq22.com/demo/jquery-lyb20151124/img/face6.gif",
            "https://www.jq22.com/demo/jquery-lyb20151124/img/face7.gif",
            "https://www.jq22.com/demo/jquery-lyb20151124/img/face8.gif"
            ]

        }
    },
    methods:{
        submit() {
            if(!this.form.username || !this.form.message) {
                alert('请输入用户名和留言')
                return
            }
            this.$emit('submit',{
                ...this.form,
                avatar:this.avatarList[this.avatarIndex]
            })
            this.form = {
                username:'',
                message:''
            }
            this.avatarIndex = 0
        }
    },
    components:{
        Avatar
    },
    computed:{
        textNum() {
            if(this.form.message.length <=140) {
                return 140 - this.form.message.length
            }else{
                return this.form.message.length - 140
            }
        }
    }
}
</script>

<style lang='scss' scoped>
.user{
    margin-top: 10px;
    display: flex;
    input{
        width: 157px;
        height: 28px;
        border: 1px solid #d0d0d0;
        padding: 0 8px;
        outline: none;
        border-radius: 4px;
        margin-right: 8px;
    }
}
.desc{
    margin: 10px 0;
    textarea{
        width: 100%;
        height: 73px;
        border: 1px solid #ccc;
        resize: none;
        outline: none;
        padding: 10px;
        border-radius: 5px;
    }
}
.btn-wrap{
    display: flex;
    justify-content: flex-end;
    align-items: center;
    button{
        width: 80px;
        height: 30px;
        margin-left: 20px;
    }
}
</style>